<?php
namespace App\Service\Admin\Ywmall;

class GoodsService extends BaseService
{
    //商品

    //列表
    public static function index()
    {
        $db = self::dbConn(self::$dbYwGoods)->where('del', 0);
        $total = $db->count();
        $res = $db->orderBy('id', 'asc')->get();
        if (!count($res)) {
            return self::toback(0, '', [], $total);
        }
        $arys = [];
        foreach ($res as $v) {
            $arys[] = [
                'id' => $v->id, 'title' => $v->gname,
                'cover' => file_exists($v->cover) ? $v->cover : '',
                'price' => toNumber($v->price),
                'num' => $v->num,
            ];
        }
        return self::toback(0, '', $arys, $total);
    }

    /**
     * 新增
     * @param array $pas
     */
    public static function add($pas)
    {
        $pas['uptime'] = $pas['addtime'] = toDate();
        $res = self::dbConn(self::$dbYwGoods)->insertGetId($pas);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }

    /**
     * 修改
     * @param int $id
     * @param array $pas
     */
    public static function modify($id, $pas)
    {
        $pas['uptime'] = toDate();
        $up = self::dbConn(self::$dbYwGoods)
            ->where('id', $id)
            ->update($pas);
        if (!$up) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }

    /**
     * 删除
     * @param id $id 场景ID
     * return array
     */
    public static function del($id)
    {
        self::dbConn(self::$dbYwGoods)
            ->where('id', $id)
            ->update(['del' => 1, 'deltime' => toDate()]);
        return self::toback();
    }
}